package im

import (
    "encoding/json"
	"dhfshop/domain/im"
	"dhfshop/controllers/base"
	"thirdpaysdk/api/qcurepay"
	"github.com/astaxie/beego"
	"dhfshop/models/isv"
	"dhfshop/domain/pay"
	"dhfshop/service/redisgo"
)

type QcurePayController struct {
	base.BaseController
}

func (this *QcurePayController) QcurePayNotify() {
	notifyData := this.Ctx.Input.RequestBody
	beego.Debug("QcurePayNotify notifyData:", string(notifyData))
	rspMap, err := qcurepay.ParseQcureNotify(notifyData)
	if err != nil {
	    beego.Debug("ParseQcureNotify:", err.Error())
		return
	}
	acPay, err := isv.GetAcPayParamsBySubMchId(rspMap["merchCode"].(string))
	if err != nil {
	    beego.Debug("GetAcPayParamsBySubMchId:", err.Error())
		return
	}
	reqMap, err := qcurepay.VerifyQcureSign(rspMap, acPay.Appkey)
	if err != nil {
	    beego.Debug("VerifyQcureSign:", err.Error())
		return
	}
	var payInfoCache *paylayer.PayInfoCache = new(paylayer.PayInfoCache)
	cacheKey := "payorder_" + reqMap["merchOrderNo"]
	redisconn := redisgo.GetInstance()
	if cacheValue, err := redisconn.GetString(cacheKey); err == nil && len(cacheValue) > 5  {
		if err = json.Unmarshal([]byte(cacheValue), payInfoCache); err == nil {
			
		}
	}else{
	    beego.Debug("QcurePayNotify cache pay info err:", err.Error())
		return 
	}
	reqMap["LocalKind"] = "QCUREPAY_NOTIFY"
	reqMap["attach"] = payInfoCache.OrderKind
	imlayer.PushMsgSvrIns.PushDao <- &reqMap
	this.Ctx.WriteString("SUCCESS")
	return
}
